package com.liddhome.controller.admin;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.liddhome.entity.Adpic;
import com.liddhome.entity.PageBean;
import com.liddhome.service.AdpicService;
import com.liddhome.util.DateUtil;
import com.liddhome.util.ResponseUtil;
import com.liddhome.util.StringUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
/**
 * 广告图片管理员Controller(控制)层
 * @author lidd
 *
 */
@Controller
@RequestMapping("/admin/adpic")
public class AdpicAdminController {

	@Resource
	private AdpicService adpicService;
	
	/**
	 * 增加或修改广告图片内容
	 * @param adpic
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/save",method=RequestMethod.POST)
	public String save(ModelMap map,String state,Adpic adpic,HttpServletRequest request,HttpServletResponse response,@RequestParam MultipartFile[] fileItem)throws Exception{
		int resultTotal=0; // 操作的记录条数
		
		if(adpic.getId() == null && state == null) {
			if(fileItem[0].getSize()<=0){
				map.addAttribute("msg", "请先选择您要上传的图片！");
				return "admin/uploadAdpic";
			}
			String filename = fileItem[0].getOriginalFilename();
			filename = DateUtil.getCurrentDateStr()+"_"+filename;
			String filePath=request.getServletContext().getRealPath("/");
			String savepath = filePath+"static/userImages/carousel/";
			File destFile = new File(savepath,filename);
			fileItem[0].transferTo(destFile);
			
			adpic.setId(DateUtil.getCurrentDateStr());
			adpic.setImage(filename);
			
			//添加广告图片
			resultTotal=adpicService.add(adpic);
			map.addAttribute("imageName", filename);
		}else {
			//修改广告图片
			resultTotal=adpicService.update(adpic);
		}
			
		if(resultTotal>0){
			map.addAttribute("msg", "上传成功！");
		}else{
			map.addAttribute("msg", "上传失败！");
		}
		return "admin/uploadAdpic";
	}
	
	/**
	 * 分页查询广告图片信息
	 * @param page
	 * @param rows
	 * @param s_customer
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/list")
	public String list(@RequestParam(value="page",required=false)String page,@RequestParam(value="rows",required=false)String rows,String s_uploadDate,String state,HttpServletResponse response)throws Exception{
		PageBean pageBean=new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
		Map<String,Object> map=new HashMap<String,Object>();
		
		if(StringUtil.isNotEmpty(s_uploadDate)) {
			map.put("uploadDate", StringUtil.formatLike(s_uploadDate));
		}else if(StringUtil.isNotEmpty(state)) {
			map.put("state", state);
		}
		
		map.put("start", pageBean.getStart());
		map.put("size", pageBean.getPageSize());
		List<Adpic> adpicList=adpicService.list(map);
		Long total=adpicService.getTotal(map);
		JSONObject result=new JSONObject();
		JsonConfig jsonConfig=new JsonConfig();
		jsonConfig.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor("yyyy-MM-dd"));
		JSONArray jsonArray=JSONArray.fromObject(adpicList,jsonConfig);
		result.put("rows", jsonArray);
		result.put("total", total);
		ResponseUtil.write(response, result);
		return null;
	}
	
	/**
	 * 删除广告图片记录
	 * @param ids
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/delete")
	public String delete(@RequestParam(value="ids")String ids,HttpServletRequest request,HttpServletResponse response)throws Exception{
		String []idsStr=ids.split(",");
		String filePath=request.getServletContext().getRealPath("/");
		String savepath = filePath+"static/userImages/carousel/";    //保存图片的路径
		for(int i=0;i<idsStr.length;i++){
			Adpic adpic = adpicService.findById(idsStr[i]);
			new File(savepath,adpic.getImage()).delete();
			adpicService.delete(idsStr[i]);
		}
		JSONObject result=new JSONObject();
		result.put("success", true);
		ResponseUtil.write(response, result);
		return null;
	}
	
	/**
	 * 通过ID查找实体
	 * @param id
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/findById")
	public String findById(@RequestParam(value="id")String id,HttpServletResponse response)throws Exception{
		Adpic adpic=adpicService.findById(id);
		JSONObject jsonObject=JSONObject.fromObject(adpic);
		ResponseUtil.write(response, jsonObject);
		return null;
	}
	
	/**
	 * 评论审核
	 * @param comment
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/review")
	public String review(@RequestParam(value="ids")String ids,@RequestParam(value="state")Integer state,HttpServletResponse response)throws Exception{
		String []idsStr=ids.split(",");
		for(int i=0;i<idsStr.length;i++){
			Adpic adpic=new Adpic();
			adpic.setState(state);
			adpic.setId(idsStr[i]);
			adpicService.update(adpic);
		}
		JSONObject result=new JSONObject();
		result.put("success", true);
		ResponseUtil.write(response, result);
		return null;
	}
}
